﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.CApAIVCSweptVolume
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SweptVolume;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;

internal class CApAIVCSweptVolume(ITxSweptVolumeHoldingObject svhObject) : CApSVSweptVolume(svhObject)
{
  private CApAIVCLogMessageEventHandler m_logMessage;
  private Regex C_REGEX_CONTROLLER_MESSAGE = new Regex("^\\[(.*)\\]\\[(.*)\\]\\[(DBG|Note|Warning|Error|Internal Error)\\](.*)$", RegexOptions.Singleline);

  internal CApAIVCLogMessageEventHandler LogMessage
  {
    set => this.m_logMessage = value;
  }

  protected override void initSamplingManager(bool includeCables)
  {
    base.initSamplingManager(includeCables);
    this.m_samplingManager.SimulationHandler.AutomaticErrorOutput = false;
    this.m_samplingManager.SimulationHandler.ForceReset = true;
  }

  protected override void onSamplingEnded()
  {
    List<string> errors1 = this.m_samplingManager.SimulationHandler.Errors;
    if (errors1.Count > 0)
    {
      ArrayList errors2 = new ArrayList();
      for (int index = 0; index < errors1.Count; ++index)
      {
        string input = errors1[index];
        Match match;
        if ((match = this.C_REGEX_CONTROLLER_MESSAGE.Match(input)) != null && match.Success)
        {
          if (match.Groups[3].Value != "DBG")
            errors2.Add((object) input);
        }
        else
          errors2.Add((object) input);
      }
      if (errors2.Count > 0)
        this.FireSimulationLogMessage(errors2);
    }
    if (!(this.m_samplingManager.SimulationHandler as CApSVOrderedLocationsSimulationHandler).ReachedAllLocations)
      this.FireSimulationNotEndedLogMessage();
    base.onSamplingEnded();
  }

  protected override bool canCreateEmptySweptVolume()
  {
    this.FireEmptySweptVolumeLogMessage();
    return false;
  }

  protected override string generateSweptVolumeObjectName(
    ITxSweptVolumeHoldingObject svhObject,
    double accuracy,
    double clearance)
  {
    ITxRobot itxRobot = (ITxRobot) null;
    switch (svhObject)
    {
      case ITxRoboticOrderedCompoundOperation _:
        itxRobot = (svhObject as ITxRoboticOrderedCompoundOperation).Robot;
        break;
      case ITxRoboticProgram _:
        itxRobot = (svhObject as ITxRoboticProgram).Robot;
        break;
    }
    return $"SV_{((ITxObject) itxRobot).Name}_{((ITxObject) svhObject).Name}";
  }

  protected override void setDisplayMode(TxSweptVolume sweptVolume)
  {
    base.setDisplayMode(sweptVolume);
    sweptVolume.Blank();
  }

  private void FireLogMessage(CApAIVCLogMessage message)
  {
    if (this.m_logMessage == null || message == null)
      return;
    this.m_logMessage((object) this, new CApAIVCLogMessageEventArgs(message));
  }

  private void FireSimulationLogMessage(ArrayList errors)
  {
    this.FireLogMessage(new CApAIVCLogMessage(eApAIVCLogCode.SIMULATION_ERROR, ((ITxObject) this.m_svhObject).Name, errors));
  }

  private void FireEmptySweptVolumeLogMessage()
  {
    this.FireLogMessage(new CApAIVCLogMessage(eApAIVCLogCode.SWEPT_VOLUME_EMPTY, ((ITxObject) this.m_svhObject).Name));
  }

  private void FireSimulationNotEndedLogMessage()
  {
    this.FireLogMessage(new CApAIVCLogMessage(eApAIVCLogCode.SIMULATION_NOT_ENDED, ((ITxObject) this.m_svhObject).Name));
  }
}
